package activity;

import java.util.ArrayList;

import com.android.volley.VolleyError;
import com.qzhy.female.R;

import Constant.Constance;
import adapter.MyFansAdapter;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import model.Fans;
import model.Fans_status;
import utils.GsonUtils;
import utils.HTTPUtils;
import volley.VolleyListener;
import widget.MyListView;
import widget.MyListView.onRefreshListener;

public class MyFansActivity extends ActionBarActivity implements OnClickListener {

	private ImageView mImgBack;
	private MyListView mPrListview;
	private ArrayList<Fans> mFans=new ArrayList<Fans>();
	private MyFansAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_fans);
		InitUI();
		GetDate();
		InitList();
	}

	private void InitUI() {
		mImgBack = (ImageView) findViewById(R.id.img_back);
		mImgBack.setOnClickListener(this);
	}

	private void GetDate() {
		int UserId = getIntent().getIntExtra(Constance.INTENT.USER_ID, 0);
		
		HTTPUtils.get(getApplicationContext(), Constance.URL.USER+"/"+UserId+"/followers", new VolleyListener() {
			@Override
			public void onResponse(String arg0) {
//				Toast.makeText(getApplicationContext(), arg0.toString(),1).show();
				Fans_status fans_status = GsonUtils.parseJSON(arg0, Fans_status.class);
				if(fans_status.getStatus().equals(Constance.STATUS_TYPE.SUCCESS)){
					mFans.clear();
					mFans.addAll(fans_status.getResult().getFollowers());
					adapter.notifyDataSetChanged();
				}
			}
			
			@Override
			public void onErrorResponse(VolleyError arg0) {
				
			}
		});
	}

	private void InitList() {
		mPrListview = (MyListView) findViewById(R.id.ptrListView);
		adapter = new MyFansAdapter(this,mFans);
		
		mPrListview.setAdapter(adapter);
		mPrListview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(MyFansActivity.this,MineActivity.class);
				startActivity(intent);
			}
		});
		mPrListview.setOnRefreshListener(new onRefreshListener() {
			@Override
			public void refresh() {
				new GetDataTask().execute();
			}
			@Override
			public void loadMore() {
				mPrListview.onRefreshComplete();
			}
		});
	}
	private class GetDataTask extends AsyncTask<Void, Void, String> {
		protected String doInBackground(Void... params) {
			try {
				Thread.sleep(2000);
				GetDate();
			} catch (InterruptedException e) {
			}
			return "";
		}

		protected void onPostExecute(String result) {
			mPrListview.onRefreshComplete();
		}
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_back:
			finish();
			break;
		default:
			break;
		}
	}
}